package com.gable.common.util;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by mj on 2017/5/26.
 */
public class StringUtil {

    /**
     * 判断字符串是否为空
     * @param string
     * @return
     */
    public static Boolean isEmpty(String string){
        if(!StringUtils.isEmpty(string) && !"null".equals(string) && !"undefined".equals(string)){
            if(!StringUtils.isEmpty(string.trim()))
                return false;
        }
        return true;
    }

    /**
     * 校验手机号码或固话
     * @param mobileNumber
     * @return
     */
    public static boolean checkMobileNumber(String mobileNumber){
        boolean flag = false;
        try{
            Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-9]))|(18[0-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Matcher matcher = regex.matcher(mobileNumber);
            boolean flag1 = matcher.matches();

            Pattern regex1 = Pattern.compile("(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +
                    "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)");
            matcher = regex1.matcher(mobileNumber);
            boolean flag2 = matcher.matches();

            if(flag1 || flag2)
                return true;
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }
}
